#include "getdata.h"
#include "stdlib.h"

struct tm timeinfo;
extern unsigned char sec_cnt;
extern unsigned char min_cnt;
extern unsigned char hour_cnt;
extern unsigned char Real_hour;
//天气 温度 城市
char City[]="成都";
char temp=0;
unsigned char weather_code=0;
//时间
int year=0;
unsigned char month=0;
unsigned char day=0;
unsigned char xingqiji=0;

const char *ssid="sense_long";
const char *password="123456789";

const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;

unsigned char j=0;
//连接wifi
void wifi_connect(void){
    WiFi.begin(ssid,password);
    while(WiFi.status()!=WL_CONNECTED){
        j++;
        delay(100);
        printf(".");
        if(j>20) break;
    }
    if(j<=20){
        printf("WiFi connected\r\n");
        printf("IP address:");
        printf("%d",WiFi.localIP());
    }
}

 // 从网络时间服务器上获取并设置时间
 // 获取成功后芯片会使用RTC时钟保持时间的更新
void time_init(void){
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}


//获取和设置时间
uint8_t get_and_set_time(void){
    if (!getLocalTime(&timeinfo))
    {
        return 0;
    }
    min_cnt=timeinfo.tm_min;
    Real_hour=timeinfo.tm_hour;
    Real_hour_to_hout_cnt();
    sec_cnt=timeinfo.tm_sec;
    day=timeinfo.tm_mday;
    month=(timeinfo.tm_mon+1);
    year=(timeinfo.tm_year+1900);
    xingqiji=timeinfo.tm_wday;
    return 1;
}

DynamicJsonDocument doc(1024);

String tagethttp = "https://api.seniverse.com/v3/weather/now.json?key=SbPafAepQpIKHm3c9&location=ip&language=zh-Hans&unit=c";

void getCityandWeather(void){
    HTTPClient http;
    http.begin(tagethttp);
    int httpCode=http.GET();//如果成功获取，返回200；失败返回负值
    //成功获取时间
    if(httpCode==HTTP_CODE_OK){
        String resBuff=http.getString();//获取网页字符串
        Serial.println(resBuff);
        deserializeJson(doc,resBuff);//开始使用json解析 
        temp=doc["results"][0]["now"]["temperature"];
        strcpy(City,doc["results"][0]["location"]["name"]);
        weather_code=doc["results"][0]["now"]["code"];
        Serial.printf("temp=%d weather_code=%d",temp,weather_code);
    }
}


/* void SmartConfig()
{
   WiFi.mode(WIFI_STA);
   Serial.println("\r\n wait for smartconfig....");
   WiFi.beginSmartConfig();
   while(1)
   {
    Serial.print(".");
    delay(500);
    if ( WiFi.smartConfigDone())
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n",WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n",WiFi.psk().c_str());
      break;      
    }
   }  
}


bool AutoConfig()
{
  WiFi.begin();
  for (int i=0; i<20; i++)
  {
    int wstatus = WiFi.status();
    if (wstatus == WL_CONNECTED )  
       {
          Serial.println("wifi smartConfig success");
          Serial.printf("SSID:%s",WiFi.SSID().c_str());
          Serial.printf(",PWS:%s\r\n",WiFi.psk().c_str());
          Serial.print("localIP:");
          Serial.println(WiFi.localIP());
          Serial.print(",GateIP:");
          Serial.println(WiFi.gatewayIP());
          return true;
          
       }
       else
       {
          Serial.print("WIFI AutoConfig Waiting ....");
          Serial.println(wstatus);
          delay(1000);
        
       }
    
  }
  Serial.println("Wifi autoconfig faild!");
  return false;
}
 */